package com.atguigu.gmall.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller

public class IndexController {


    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private TemplateEngine templateEngine;  //首页即模板

    @GetMapping("/")//"/"代表首页
    public String index(Model model){
        List<Map> mapList = getData();

        model.addAttribute("list",mapList);
        //首页
        return "index/index";
    }

    public List<Map> getData() {
        List<Map> mapList = new ArrayList<>();

        //所有分类
        List<BaseCategoryView> baseCategoryViewList = productFeignClient.getBaseCategoryViewList();
        //根据一级分类id进行分组，返回值 Map
        Map<Long, List<BaseCategoryView>> baseCategoryViewListMap = baseCategoryViewList.
                stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));

        //1、亲前60个数据算一个Map
        baseCategoryViewListMap.forEach((k,v)->{
            //一级分类
            Map category1Map = new HashMap<>();
            category1Map.put("index",k);
            category1Map.put("categoryId",k);
            category1Map.put("categoryName",v.get(0).getCategory1Name());
            //二级分类
            Map<Long, List<BaseCategoryView>> baseCategoryView2ListMap =
                    v.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
            List<Map> mapList2 = new ArrayList<>();
            baseCategoryView2ListMap.forEach((k2,v2)->{
                Map category2Map = new HashMap<>();
                category2Map.put("categoryId",k2);
                category2Map.put("categoryName",v2.get(0).getCategory2Name());
                //三级分类
                List<Map> mapList3 = new ArrayList<>();
                v2.forEach(BaseCategoryView->{
                    Map category3Map = new HashMap<>();
                    category3Map.put("categoryId",BaseCategoryView.getCategory3Id());
                    category3Map.put("categoryName",BaseCategoryView.getCategory3Name());
                    mapList3.add(category3Map);
                });

                category2Map.put("categoryChild",mapList3);
                mapList2.add(category2Map);
            });

            category1Map.put("categoryChild",mapList2);
            mapList.add(category1Map);
        });
        return mapList;
    }


    //使用静态化程序生成首页，保存在指定的盘符下，盘符挂载到Nginx服务上去
    @GetMapping("/creatHtml")
    @ResponseBody
    public Result creatHtml() throws IOException {
        //首页的生成（数据+模板==输出流,即将页面保存到指定的磁盘上）
        //参数一：模板 (即我们的index页面，不用重新写了)
        //参数二：数据
        Context context = new Context();
        context.setVariable("list",getData());
        //参数三：输出流   写
        //Writer writer = new OutputStreamWriter(new FileOutputStream("D:\\temp\\index.html"),"utf-8");//要生成在哪个页面下，并且文件名字是什么

        PrintWriter writer = new PrintWriter("D:\\temp\\index.html","utf-8");

        //静态化程序  读（已经在yml文件中配置了）
        templateEngine.process("index/index",context,writer);
        return Result.ok();


    }






}
